export default function (url, fd = new FormData()) {
    return new Promise((resolve, reject) => {
        const xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4) {
                if (xhr.status == 200) {
                    // console.log(
                    //     JSON.parse(xhr.response)
                    // );
                    resolve(JSON.parse(xhr.response))
                } else {
                    if (xhr.status == 401) {
                        alert('您未登录\n请重新登录');
                        location = '/login.html?url=' + encodeURIComponent(location.href);
                    } else {
                        alert('服务器错误' + xhr.status);
                        reject(xhr.status);
                    }
                }
            }
        };
        xhr.open('post', url + '?a=' + new Date(), true);
        xhr.setRequestHeader('Authorization', 'Bearer ' + localStorage.getItem('token'));
        xhr.send(fd);
    })
}